package com.bd.wechat.utils;

import com.bd.wechat.core.LoginMember;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

public class UserIdUtil {
    public static Long getCurrentUserId() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

        if (authentication != null && authentication.getPrincipal() instanceof UserDetails) {
            LoginMember loginMember = (LoginMember) authentication.getPrincipal();
            // 假设你的UserDetails实现类有一个getUserId()方法
            return (Long) loginMember.getMemberId(); // 或者其他方式获取ID
        }

        return null; // 或者抛出异常，表示没有当前用户
    }
}
